package com.dataaccess.dataaccess.support.hikvision;

import cn.hutool.json.JSONObject;
import com.hikvision.artemis.sdk.ArtemisHttpUtil;
import com.hikvision.artemis.sdk.config.ArtemisConfig;
import org.apache.http.HttpResponse;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: YangYu
 * @ClassName: Test
 * @Date: Created in 14:20 2021/3/17
 * @Description:
 */
public class GetCameraPreviewURL {

    public static String GetCameraPreviewURL() throws IOException {

        /**
         * STEP1：设置平台参数，根据实际情况,设置host appkey appsecret 三个参数.
         */
        ArtemisConfig.host = "122.4.227.102:4433"; // artemis网关服务器ip端口
        ArtemisConfig.appKey = "24769112";  // 秘钥appkey
        ArtemisConfig.appSecret = "NxjYIfempoqQaVtRqKLF";// 秘钥appSecret

        /**
         * STEP2：设置OpenAPI接口的上下文
         */
        final String ARTEMIS_PATH = "/artemis";

        /**
         * STEP3：设置接口的URI地址
         */
        final String previewURLsApi = ARTEMIS_PATH + "/api/frs/v1/application/picture";
        Map<String, String> path = new HashMap<String, String>(2) {
            {
                put("https://", previewURLsApi);//根据现场环境部署确认是http还是https
            }
        };

        /**
         * STEP4：设置参数提交方式
         */
        String contentType = "application/json";

        /**
         * STEP5：组装请求参数
         */
        JSONObject jsonBody = new JSONObject();
//        List<Integer> eventTypes = new ArrayList<>();
//        eventTypes.add(131594);
//        eventTypes.add(131330);
//        List<Integer> eventLvl = new ArrayList<>();
//        eventLvl.add(1);
//        eventLvl.add(2);
//        eventLvl.add(3);
//        jsonBody.put("eventTypes", eventTypes);
//        jsonBody.put("eventDest", "http://121.89.184.226:8101/video/hikvision");
//        jsonBody.put("subType", 2);
//        jsonBody.put("eventLvl", eventLvl);
//        jsonBody.put("cameraIndexCode", "e564e48a87bd44d1b731703f609d1f6e");
//        jsonBody.put("protocol", "rtsp");
//        jsonBody.put("expand", "streamform=rtp");
        jsonBody.put("url", "/pic?3d42=309o498-68*917=43b9567661t6==121***ps==3a0*cbb2295f8-8d77747-19l3eb-25=765*69621");
        String body = jsonBody.toString();
        /**
         * STEP6：调用接口
         */
        ArtemisHttpUtil.doPostStringImgArtemis(path, body, null, null, contentType , null);
        HttpResponse result = ArtemisHttpUtil.doPostStringImgArtemis(path, body, null, null, contentType , null);// post请求application/json类型参数
        InputStream inputStream = result.getEntity().getContent();
        copyInputStreamToFile(inputStream, new File("d://aa.jpg"));
        System.out.println(result);
        return null;
    }
    // InputStream -> File
    private static void copyInputStreamToFile(InputStream inputStream, File file)
            throws IOException {
        //file = new File("d://aa.jpg");
        try (FileOutputStream outputStream = new FileOutputStream(file)) {

            int read;
            byte[] bytes = new byte[1024];

            while ((read = inputStream.read(bytes)) != -1) {
                outputStream.write(bytes, 0, read);
            }

            // commons-io
            //IOUtils.copy(inputStream, outputStream);

        }

    }

    public static void main(String[] args) throws IOException {

        String result = GetCameraPreviewURL();
        System.out.println("result结果示例: " + result);
        //101.201.140.249:8101/video/hikvision

//        JSONObject jsonObject = new JSONObject();
//        List<Integer> list = new ArrayList<>();
//        list.add(1);
//
//        jsonObject.put("eventTypes",list);
//        jsonObject.put("eventDest","123");
//
//        jsonObject.put("subType",1);
//        jsonObject.put("eventDest","123");
//        System.out.println(jsonObject);
    }
}
